<?php
App::import('Sanitize');

class RecipesController extends AppController
{
  //var $scaffold;
  var $uses = array("Account", "Category", "Comment", "CookingMethod", "Favorite", "Recipe");
  var $helpers = array("Upload", "Javascript", "Form", "Html", "Ajax", "Teefan", "CkEditor");
  var $components = array("RequestHandler");
  
  function index()
  { 
    $highlighted_recipe = $this->Recipe->find("first", array("conditions" => array("Recipe.approved" => "1"), "order" => "rand()"));
    $this->set("highlighted_recipe", $highlighted_recipe);
    
    $highlighted_cooking_method = $this->CookingMethod->find("first", array("order" => "rand()"));
    $this->set("highlighted_cooking_method", $highlighted_cooking_method);
    
    $this->pageTitle = "Nghệ Thuật Nấu Ăn";
  }
  
  function add_to_favorite($id = null)
  {
    if ($id == null || !$this->Session->check("Account.logged") || $this->Session->read("Account.logged") == "NO")
    {
      $this->redirect("/accounts/register");
    }
    else
    {
      $favorite = $this->Favorite->findByAccountIdAndRecipeId($this->Session->read("Account.id"), $id);
      $recipe = $this->Recipe->findById($id);
      if (!empty($recipe) && empty($favorite))
      {
        $this->data["Favorite"]["account_id"] = $this->Session->read("Account.id");
        $this->data["Favorite"]["recipe_id"] = $id;
        if ($this->Favorite->save($this->data))
        {
          $this->redirect("/recipes/show/".$recipe["Recipe"]["permalink"]);
        }
        else
        {
          $this->redirect("/recipes/show_all");
        }
        
      }
      else
      {
        $this->redirect("/recipes/show_all");
      }
      
    }
  }
  
  function remove_from_favorite($id = null)
  {
    if ($id == null || !$this->Session->check("Account.logged") || $this->Session->read("Account.logged") == "NO")
    {
      $this->redirect("/accounts/register");
    }
    else
    {
      $favorite = $this->Favorite->findByAccountIdAndRecipeId($this->Session->read("Account.id"), $id);
      $recipe = $this->Recipe->findById($id);
      if (!empty($favorite))
      {
        if ($this->Favorite->delete($favorite["Favorite"]["id"]))
        {
          $this->redirect("/recipes/show/".$recipe["Recipe"]["permalink"]);
        }
        else
        {
          $this->redirect("/recipes/show_all");
        }
      }
      else
      {
        $this->redirect("/recipes/show_all");
      }
      
    }
  }
  
  function show_all()
  {
    $categories = $this->Category->find("all", array("order" => "Category.order ASC"));
    $this->set("categories", $categories);
    
    $recipes_count = $this->Recipe->find("count");
    $this->set("recipes_count", $recipes_count);
    
    $this->pageTitle = "Danh Sách Món Ăn";
  }
  
  function add()
  {
    if (!empty($this->data))
    {
      //$this->data = Sanitize::clean($this->data, array('encode' => false, 'allow' => '\n'));
      if ($this->Recipe->save($this->data))
      {
        $latest_recipe = $this->Recipe->find("first", array("order" => "Recipe.id DESC"));
        if (!empty($latest_recipe))
        {
          $this->redirect("/recipes/add_complete/".$latest_recipe["Recipe"]["permalink"]);
        }
      }
      else
      {
        
      }
    }
    $categories = array(-1 => 'Chọn loại món ăn');
    $categories += $this->Category->find("list");
    
    $this->set("categories", $categories);
    
    $this->pageTitle = "Chia Sẻ Món Ăn";
  }
  
  function add_complete($permalink = null)
  {
    if ($permalink == null)
    {
      $this->redirect("/recipes/show_all");
    }
    else
    {
      $recipe = $this->Recipe->findByPermalink($permalink);
      $this->set("recipe", $recipe);
    }
    
    $this->pageTitle = "Hoàn Tất Chia Sẻ Món Ăn";
  }
  
  function edit($permalink = null)
  {
    if (!$this->Session->check("Account.logged") || $this->Session->read("Account.logged") == "NO")
    {
      $this->redirect("/accounts/register");
    }
    
    if ($permalink == null)
    {
      $this->redirect("/recipes/show_all");
    }
    
    $recipe = $this->Recipe->findByPermalink($permalink);
    
    if (empty($recipe))
    {
      $this->redirect("/recipes/show_all");
    }
    else
    {
      if (!empty($this->data))
      {
        $this->Recipe->id = $recipe["Recipe"]["id"];
        //$this->data = Sanitize::clean($this->data, array('encode' => false, 'allow' => '\n'));
        if ($this->Recipe->save($this->data))
        {
          $this->redirect("/recipes/show/".$permalink);
        }
        else
        {
          
        }
      }
      else
      {
        $this->Recipe->id = $recipe["Recipe"]["id"];
        $this->data = $this->Recipe->read();
      }
      
    }
    
    $this->set("recipe_permalink", $permalink);
    $categories = $this->Category->find("list");
    $this->set("categories", $categories);
    
    $this->pageTitle = "Sửa Món Ăn";
  }
  
  function show($permalink = null)
  {
    if ($permalink == null)
    {
      $this->redirect("/recipes/show_all");
    }
    
    $recipe = $this->Recipe->findByPermalink($permalink);
    
    if (empty($recipe))
    {
      $this->redirect("/recipes/show_all");
    }
    else
    {
      $this->Recipe->id = $recipe["Recipe"]["id"];
      $this->set("recipe", $recipe);
    }
    
    $this->set("recipe_permalink", $permalink);
    $this->set("recipe_category_id", $recipe["Recipe"]["category_id"]);
    
    $favorite = $this->Favorite->findByAccountIdAndRecipeId($this->Session->read("Account.id"), $recipe["Recipe"]["id"]);
    $this->set("favorite", $favorite);
    
    $favorite_count = $this->Favorite->find("count", array("conditions" => array("Favorite.recipe_id" => $recipe["Recipe"]["id"])));
    $this->set("favorite_count", $favorite_count);
    
    $comments = $this->Comment->findAllByRecipeId($recipe["Recipe"]["id"]);
    $this->set("comments", $comments);
    
    $this->pageTitle = "Cách nấu, cách chế biến, cách làm, công thức ".$recipe["Recipe"]["name"];
  }
  
  function get_random_recipes()
  {
    return $this->Recipe->find("all", array("conditions" => array("Recipe.approved" => "1"), "order" => "rand()", "limit" => "5"));
  }
  
  function get_same_category_recipes($id = null)
  {
    return $this->Recipe->find("all", array("conditions" => array("Recipe.approved" => "1", "Recipe.category_id" => $id), "order" => "rand()", "limit" => "5"));
  }
  
  function get_recently_talked_recipes()
  {
    
  }
  
  function get_favorite_recipes()
  {
    
  }
  
}
?>
